package com.alog.javaee.day37;

import com.alog.common.utils.JdbcUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/2/16 20:35
 * @Description: 操作数据库中user表的类
 */
public class UserDao {

    private JdbcTemplate template = new JdbcTemplate(JdbcUtil.getDataSource());

    /**
     * 登陆方法
     * @return user包含用户的所有数据
     */
    public User login(User loginUser){
        try {
            String sql = "select * from user where username = ? and password = ?";
            User user = template.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class),
                    new Object[]{loginUser.getUsername(), loginUser.getPassword()});

            return user;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}
